package com.yu.mall.configuration;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class SwaggerConfiguration {

    @Bean
    @Profile(value={"dev","test"})
    public Docket createRestApi() {
        List<Parameter> parameters = new ArrayList();
        parameters.add(new ParameterBuilder().name("openid").description("微信openid")
                .modelRef(new ModelRef("string")).parameterType("header").required(false)
                .build());
        parameters.add(new ParameterBuilder().name("token").description("token")
                .modelRef(new ModelRef("string")).parameterType("header").required(false)
                .build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("接口服务")
                .apiInfo(apiInfo())
                .globalOperationParameters(parameters)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yu.mall.controller"))
                .paths(PathSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .version("1.0")
                .build();
    }
}